package mobi.bantu.utils;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import mobi.bantu.bean.xls.ExcelEntity;
import mobi.bantu.bean.xls.ExcelSheet;

/**
 * excel工具类，主要用于导入，导出
 * 
 * @author BYX
 *
 */
public class ExcelUtils2 {
	private static final Log log = LogFactory.getLog(ExcelUtils2.class);
	static SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 写文件2003
	 * 
	 * @param model
	 * @return
	 */
	public static Workbook writeExcel2003(ExcelEntity model) {
		if (model == null || model.getSheetList() == null || model.getSheetList().isEmpty()) {
			log.error("excelEntity为空！");
			return null;
		}
		Workbook book = new HSSFWorkbook();
		List<ExcelSheet> sheetList = model.getSheetList();
		for (ExcelSheet excelSheet : sheetList) {
			Sheet sheet = book.createSheet(excelSheet.getSheetName());
			writeSheet(sheet, excelSheet);
		}
		return book;
	}

	/**
	 * 写一个sheet
	 * 
	 * @param sheet
	 * @param excelSheet
	 */
	private static void writeSheet(Sheet sheet, ExcelSheet excelSheet) {
		Row row = null;
		Cell cell = null;
		// 标题
		if (null != excelSheet.getTitleList() && excelSheet.getTitleList().size() > 0) {
			int titleRowNum = excelSheet.getTitleRowNum();
			if (titleRowNum == 0) {
				titleRowNum = ExcelSheet.DEFAULT_TITLEROWNUM;
			}
			row = sheet.createRow(excelSheet.getTitleRowNum());

			for (int i = 0; i < excelSheet.getTitleList().size(); i++) {
				cell = row.createCell(i);
				// 获取宽度，如果没有设置则采用自动
				if (null != excelSheet.getColumnWidth() && excelSheet.getColumnWidth().length > 0
						&& excelSheet.getColumnWidth().length == excelSheet.getTitleList().size()) {
					int columnWidth = excelSheet.getColumnWidth()[i];
					if (columnWidth > 0) {
						sheet.setColumnWidth(i, columnWidth);
					}
				} else {
					sheet.autoSizeColumn(i);
				}
				cell.setCellValue(excelSheet.getTitleList().get(i));
			}
		}

		// 内容
		if (null != excelSheet.getContentList() && excelSheet.getContentList().size() > 0) {
			int dataRowNum = excelSheet.getContentRowNum();
			if (dataRowNum == 0) {
				dataRowNum = ExcelSheet.DEFAULT_CONTENTROWNUM;
			}
			for (List<String> contentItem : excelSheet.getContentList()) {
				row = sheet.createRow(dataRowNum++);
				if (null != contentItem && contentItem.size() > 0) {
					for (int i = 0; i < contentItem.size(); i++) {
						cell = row.createCell(i);
						cell.setCellValue(contentItem.get(i));
					}
				}

			}
		}

	}

    //excel表单
    public static ExcelSheet createExcelSheet(String sheetname, String[] head, List<List<String>> contentlst) {
        //表单实体
        ExcelSheet excelSheet = new ExcelSheet();
        excelSheet.setSheetName(sheetname);
        //首行标题
        List<String> titleList = Arrays.asList(head);
        excelSheet.setTitleList(titleList);
        //每一行内容
        excelSheet.setContentList(contentlst);
        //单元格宽度
        int[] columnWidth = new int[] { 5000, 5000 };
        excelSheet.setColumnWidth(columnWidth);
        return excelSheet;
    }
}
